/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.util.jpa;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import kiff.util.ClassUtils;
import kiff.util.Provider;

import com.google.inject.Inject;

/**
 * Entity Manager Provider.
 * @author Adam
 * @version $Id: EntityManagerProvider.java 62 2008-10-31 04:51:50Z a.ruggles $
 * 
 * Created on Oct 8, 2008 at 7:27:30 PM 
 */
public class EntityManagerProvider implements Provider<EntityManager> {
	/**
	 * A Proxy instance of the entity manager that is thread-safe.
	 */
	private final EntityManager entityManagerProxy;

	/**
	 * Constructs an EntityManager provider.
	 * @param entityManagerFactory The EntityManagerFactory.
	 */
	@Inject
	public EntityManagerProvider(final EntityManagerFactory entityManagerFactory) {
		InvocationHandler handler = new EntityManagerProxy(entityManagerFactory);
		entityManagerProxy = (EntityManager) Proxy.newProxyInstance(ClassUtils.getClassLoader(),
				new Class[] { EntityManager.class }, handler);
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Provider#get()
	 */
	public EntityManager get() {
		return entityManagerProxy;
	}
}
